package com.springboot.cloud.sysadmin.organization.events;

import com.springboot.cloud.sysadmin.organization.config.BusConfig;
import com.springboot.cloud.sysadmin.organization.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RabbitListener(queues = BusConfig.QUEUE_NAME_R)
public class RoleEventReceiver {

    @Autowired
    private IUserService userService;

    @RabbitHandler
    public void process(String roleId){
        log.debug("mq 接收到的消息{}",roleId);
        List<String> usernames=userService.findUsernameByRoleId(roleId);
        usernames.forEach(username ->{
            userService.refresh(username);
            log.debug("清理了{}权限缓存",username);
        });
    }
}
